#include "wap_editline.h"
#include "wa_wm.h"
#include "wa_win.h"
#include "wa_screen.h"
#include "wa_editline.h"
#include "wap.h"

static void wap_editline_create(void);
static void wap_editline_del(void);
static void wap_editline_callback(wa_sign* sign);

wa_screen_obj wap_editline_screen = 
{
	.id = WAP_WIN_EDITLINE_ID,
	.create = wap_editline_create,
	.del = wap_editline_del,
};

wa_obj* wap_editline_win;
wa_obj* wap_editline_editline;

extern wa_font font_song_16_ascii;

int8_t wap_editline_focus;
char wap_editline_str[17] = "1234567890ABCDEF";

void wap_editline_init(void)
{
	wap_editline_focus = -1;
}

static void wap_editline_create(void)
{
	wap_editline_win = wa_win_create(NULL);
	wa_win_set_pos(wap_editline_win, 0, 0);
	wa_win_set_size(wap_editline_win, WA_LCD_W, WA_LCD_H);
	wa_win_set_edge_show(wap_editline_win, wa_nok);
	wa_win_set_callback(wap_editline_win, wap_editline_callback);
	wap_editline_screen.win = wap_editline_win;
	
	wap_editline_editline = wa_editline_create(NULL);
	wa_win_add_obj(wap_editline_win, wap_editline_editline);
	wa_obj_set_pos(wap_editline_editline, 0, 16);
	wa_obj_set_size(wap_editline_editline, 128, 16);
	wa_editline_set_font(wap_editline_editline, &font_song_16_ascii);
	wa_editline_set_fore_color(wap_editline_editline, WA_COLOR_FOREGROUND);
	wa_editline_set_back_color(wap_editline_editline, WA_COLOR_BACKGROUND);
	wa_editline_set_align_mode(wap_editline_editline, WA_EDITLINE_AL_LEFT | WA_EDITLINE_AL_TOP);
	wa_editline_set_text(wap_editline_editline, wap_editline_str);
	wa_editline_set_row_space(wap_editline_editline, 0);
	wa_editline_set_col_space(wap_editline_editline, 0);
	wa_editlint_set_focus(wap_editline_editline, wap_editline_focus);
}

static void wap_editline_del(void)
{
	wa_obj_del(wap_editline_win);
}

static void _wap_editline_paint(wa_obj* obj)
{
}

static void wap_editline_callback(wa_sign* sign)
{
	int sign_id;
	wa_obj* obj;
	sign_id = sign->sign_id;
	obj = sign->obj_dst;

	switch (sign_id)
	{
		case WA_SIGNAL_PAINT:
		{
			_wap_editline_paint(obj);
			break;
		}
		case WA_SIGNAL_NOTIFY_PARENT:
		{	
			break;
		}
		case WA_SIGNAL_USER_KEY_LEFT:
		{	
			if(wap_editline_focus > -1)
			{
				wap_editline_focus--;
			}
			wa_editlint_set_focus(wap_editline_editline, wap_editline_focus);
			break;
		}
		case WA_SIGNAL_USER_KEY_RIGHT:
		{	
			if(wap_editline_focus < 15)
			{
				wap_editline_focus++;
			}
			wa_editlint_set_focus(wap_editline_editline, wap_editline_focus);
			break;
		}
		case WA_SIGNAL_USER_KEY_UP:
		{	
			char temp;
			if(wap_editline_focus >= 0)
			{
				temp = (char)wa_editline_get_focus_letter(wap_editline_editline);
				if(temp < 126)
				{
					temp++;
				}
				wap_editline_str[wap_editline_focus] = temp;
				wa_editline_set_text(wap_editline_editline, wap_editline_str);
			}
			break;
		}
		case WA_SIGNAL_USER_KEY_DOWN:
		{	
			char temp;
			if(wap_editline_focus >= 0)
			{
				temp = (char)wa_editline_get_focus_letter(wap_editline_editline);
				if(temp > 32)
				{
					temp--;
				}
				wap_editline_str[wap_editline_focus] = temp;
				wa_editline_set_text(wap_editline_editline, wap_editline_str);
			}
			break;
		}
		case WA_SIGNAL_USER_KEY_ESC:
		{	
			wa_screen_switch(WAP_WIN_MENU_ID, WA_SCREEN_SWITCH_TYPE_LEFT_TOGETHER, 500, wa_nok, wa_nok);
			break;
		}
		default:
		{
			break;
		}
	}
}
